<template>
  <div class="content-list">
    <div class="list-title">我的订单</div>
    <el-tabs default-active-key="" @tab-click="onTabChange">
      <el-tab-pane name="0" label="全部">
      </el-tab-pane>
      <el-tab-pane name="1" label="待付款">
      </el-tab-pane>
      <el-tab-pane name="2" label="已支付">
      </el-tab-pane>
    </el-tabs>

    <div class="list-content" v-loading="loading">
      <template v-if="!orderData || orderData.length <= 0">
        <el-empty style="width: 100%;margin-top: 200px;" description="当前没有订单"/>
      </template>
      <div class="order-item-view" v-for="(item, index) in orderData" :key="index">
        <div class="header flex-view">
          <div class="left">
            <span class="text">订单号</span>
            <span>#</span>
            <span class="num">{{ item.order_id }}</span>
            <span class="time">{{ item.order_time }}</span>
          </div>
          <div class="right">
            <el-button v-if="item.buy_status==='0'"  type="success" size="small" style="margin-right: 10px;">支付</el-button>
            <el-popconfirm
                v-if="item.buy_status==='0'"
                title="确定取消订单？"
                confirm-button-text="是"
                cancel-button-text="否"
                @confirm="handleCancel(item)"
            >
              <template #reference>

                <el-button type="primary" size="small" style="margin-right: 24px;">取消</el-button>
              </template>
            </el-popconfirm>
            <span class="text">订单状态</span>
            <span class="state">{{ item.buy_status === '0' ? '待支付' : item.buy_status === '1' ? '已支付' : '已取消' }}</span>
          </div>
        </div>
        <div class="content flex-view">
          <div class="left-list">
            <div class="list-item flex-view">
              <el-image :src="item.picture_url" class="thing-img"></el-image>
              <div class="detail flex-between flex-view">
                <div class="flex-between flex-top flex-view">
                  <h2 class="name">{{ item.shop_name }}</h2>
                  <span class="count">x{{ item.quantity }}</span>
                </div>
                <div class="flex-between flex-center flex-view">
                  <span class="type"></span>

                </div>
              </div>
            </div>
          </div>
          <div class="right-info">
            <p class="title">收货信息</p>
            <p class="name">姓名：{{ item.name }}</p>
            <p class="name">手机：{{ item.mobile }}</p>
            <p class="name">地址：{{ item.province }}{{ item.city }}{{ item.region }}{{ item.detail }}</p>
            <p class="title">快递单号</p>
            <p class="text"></p>
            <p class="title">备注信息</p>
            <p class="text">{{ item.remark }}</p>
          </div>
        </div>
        <div class="bottom flex-view">
          <div class="left">
            <span class="text">共{{ item.quantity }}件</span>
            <span class="open" @click="handleDetail(item.shop_id,item.kind)">详情</span>
          </div>
          <div class="right flex-view">
            <span class="text">总计</span>
            <span class="num">¥ {{ item.totalprice }}</span>
            <span class="text">优惠</span>
            <span class="num">¥0</span>
            <span class="text">实际支付</span>
            <span class="money">¥ {{ item.totalprice }}</span>
          </div>
        </div>
      </div>

    </div>

  </div>
</template>

<script setup>
import {ref, onMounted} from 'vue'
import {shopCustomStore} from '@/stores/modules/shop/index.js'
import useAdminrouter from "@/router/index.js";
import {
  getOrderInfoService,
  clickCancel
} from '@/api/netshop/order.js'
import {
  CodeToText
} from 'element-china-area-data'

// const router = useRouter();
// const route = useRoute();
const store = shopCustomStore();
const router = useAdminrouter

const loading = ref(false)
const orderData = ref([])
const orderStatus = ref('')

onMounted(() => {
  getOrderList()
})

const onTabChange = (key) => {
  console.log(key.index)
  if (key.index === '0') {
    orderStatus.value = ''
  }
  if (key.index === '1') {
    orderStatus.value = '0'
  }
  if (key.index === '2') {
    orderStatus.value = '1'
  }
  getOrderList()
}
const getOrderList = async () => {
  loading.value = true
  let custom_id = store.custom_id
  await getOrderInfoService(custom_id, orderStatus.value)
      .then((res) => {
        console.log(res)
        orderData.value = res.data.data
        loading.value = false
      })
      .catch((err) => {
        console.log(err)
      })
}
const handleDetail = (thingId, kind) => {
  // 跳转新页面
  console.log(thingId, kind)
  const hasKind = kind.includes('粮')
  if (hasKind) {
    let text = router.resolve({name: 'shopdetail', query: {id: thingId}})
    window.open(text.href, '_blank')
  }else{
    let text = router.resolve({name: 'petdetail', query: {id: thingId}})
    window.open(text.href, '_blank')
  }

}
const handleCancel = async (item) => {
  await clickCancel(item.id)
      .then((res) => {
        console.log(res)
        getOrderList()
      })
      .catch((err) => {
        console.log(err)
      })
}
// 转变地址code
const getCodeToText = (codeArray) => {
  let area = "";
  switch (codeArray.length) {
    case 1:
      area += CodeToText[codeArray[0]];
      break;
    case 2:
      area += CodeToText[codeArray[0]] + "/" + CodeToText[codeArray[1]];
      break;
    case 3:
      area +=
          CodeToText[codeArray[0]] +
          CodeToText[codeArray[1]] +
          CodeToText[codeArray[2]];
      break;
    default:
      break;
  }
  return area
};
</script>
<style scoped>
.flex-view {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
}

.content-list {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
}

.list-title {
  color: #152844;
  font-weight: 600;
  font-size: 18px;
  line-height: 24px;
  height: 24px;
  margin-bottom: 4px;
}


.order-item-view {
  background: #f7f9fb;
  border-radius: 4px;
  padding: 16px;
  margin-top: 12px;
}

.header {
  border-bottom: 1px solid #cedce4;
  padding-bottom: 12px;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  font-size: 14px;
}

.text {
  color: #6f6f6f;
}

.mg-4 {
  margin-left: 4px;
}

.num {
  font-weight: 500;
  color: #152844;
}

.num {
  font-weight: 500;
  color: #152844;
}

.time {
  margin-left: 16px;
  color: #a1adc5;
}

.state {
  color: #ff7b31;
  font-weight: 600;
  margin-left: 10px;
}

.content {
  padding: 12px 0;
  overflow: hidden;
}

.left-list {
  overflow: hidden;
  height: 132px;
  -webkit-box-flex: 2;
  -ms-flex: 2;
  flex: 2;
  padding-right: 16px;
}

.list-item {
  height: 60px;
  margin-bottom: 12px;
  overflow: hidden;
  cursor: pointer;
}

.thing-img {
  width: 48px;
  height: 100%;
  margin-right: 12px;
}

.detail {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
}

.flex-between {
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}

.flex-between {
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}

.flex-top {
  -webkit-box-align: start;
  -ms-flex-align: start;
  align-items: flex-start;
}

.name {
  color: #152844;
  font-weight: 600;
  font-size: 14px;
  line-height: 18px;
}

.count {
  color: #484848;
  font-size: 12px;
}

.flex-between {
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}

.flex-center {
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}

.type {
  color: #6f6f6f;
  font-size: 12px;
}

.price {
  color: #ff7b31;
  font-weight: 600;
  font-size: 14px;
}

.right-info {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  border-left: 1px solid #cedce4;
  padding-left: 12px;
  line-height: 22px;
  font-size: 14px;
}

.title {
  color: #6f6f6f;
}

.name {
  color: #152844;
}

.text {
  color: #484848;
}

.mg {
  margin-bottom: 4px;
}

.bottom {
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  border-top: 1px solid #cedce4;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  font-size: 14px;
  padding-top: 14px;
}

.text {
  color: #6f6f6f;
}

.open {
  color: #4684e2;
  margin-left: 8px;
  cursor: pointer;
}

.right {
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}

.text {
  color: #6f6f6f;
}

.num {
  color: #152844;
  margin: 0 40px 0 8px;
}

.money {
  font-weight: 600;
  font-size: 18px;
  color: #ff7b31;
  margin-left: 8px;
}

.order-item-view:first-child {
  margin-top: 16px;
}
</style>
